﻿$(function () {
    if ($("#BirthDate").val()) {
        $("#selectYear").val($("#BirthDate").val().substr(0, 4));
        $("#selectMonth").val($("#BirthDate").val().substr(5, 2));
        $("#selectDate").val($("#BirthDate").val().substr(8, 2));
    }
    else {
        $("#selectYear").val("1987");
        $("#selectMonth").val("05");
        $("#selectDate").val("01");
    }

    //个人信息表单验证
    $("#profileForm").validate({
        rules: {
            RealName: {
                required: true
            },
            CellPhone: {
                required: true,
                isMobile: true
            },
            Address: {
                required: true
            },
            Email: {
                required: true,
                isEmail: true
            }
        },
        submitHandler: function (form) {
            $("#BirthDate").val($("#selectYear").val() + "-" + $("#selectMonth").val() + "-" + $("#selectDate").val());
            form.submit();
        }
    });

    //修改密码表单验证
    $("#pwdForm").validate({
        rules: {
            oldPwd: {
                required: true,
                minlength:6,
                maxlength: 20
            },
            newPwd: {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            txtRePassword: {
                required: true,
                minlength: 6,
                maxlength: 20,
                equalTo: "#newPwd"
            }
        },
        submitHandler: function (form) {
            changePassword();
        }
    });
 
    //改变密码
    function changePassword() {
        var $oldPwd = $.trim($("#oldPwd").val());
        var $newPwd = $.trim($("#newPwd").val());       

        var submitData = {
            oldPwd: $oldPwd,
            newPwd: $newPwd
        };
        $.ajax({
            type: "POST",
            data: submitData,
            dataType: "JSON",
            url: "ChangePasswordByAjaxRequest",
            success: function (response) {
                if (response.Result != null && response.Result.Status) {
                    KevinShop.MessageBox.Success("修改成功！");
                    return;
                }
                else if (response.Result != null && response.Result.Status == false && response.Result.ErrorMessage != null) {
                    KevinShop.MessageBox.Error(response.Result.ErrorMessage);
                    return;
                }
            },
            error: function (err) {
                alert(err.responseText);
                return;
            }
        });
    };

    $('#Tabs').aTabs({
        eventName: 'click'
    });
});